<?php
class ClsLHtmltable
{
    private $cols=1;
    private $values;
    private $arrCell=array();
    private $arrSideHeading=array();
    private $table="";
    private $captions;
    private $tableID;
    private $headColor;
    private $bodyColor;
    private $tableWidth='100%';
    private $arrRow=array();
    
    function __construct($cols=1)
    {
        $this->cols=$cols;
    }
    
    function setWidth($wid)
    {
        $this->tableWidth=$wid;
    }

    /**
     * 
     * @param type $row ($row can be both string as well as number. if $row is string, the data is retrived based on the side heading)
     * @param type $col ($col can be both string as well as number. if $col is string, the data is retrived based on the column heading)
     * @return type (returns cell object)
     */
    public function &getHtmlcell($col=-1,$row=-1)
    {
        if($row===-1 && $col===-1)
        {
            $this->arrCell[]=new ClsLHtmlcell();
            return $this->arrCell[count($this->arrCell)-1];
        }
        $pos=0;
        if($row===-1)
        {
            $pos=$col;
        }
        else if($col===-1)
        {
            return false;
        }
        else
        {
            $pos=($row*($this->cols))+$col;
        }
        return $this->arrCell[$pos];
    }

    function &addData($value)
    {
        $objCell=$this->getHtmlcell();
        $objCell->addData($value);
        return $objCell;
    }
    
    function addRow($arrValue)
    {
        foreach($arrValue as $value)
        {
            $this->addData($value);
        }
    }

    function &modifyData($value,$col,$row=-1)
    {
        $objCell=$this->getHtmlcell($col, $row);
        $objCell->addData($value);
        return $objCell;
    }

    function render($transpose=false)
    {
        if($this->tableID!="")
        {
            $tblID="id='".$this->tableID."'";
        }
        else
        {
            $tblID="";
        }
        $table="<Table $tblID bgcolor='".$this->bodyColor."' width='".$this->tableWidth."'>";
        $arrTr=array();
        if($transpose)
        {
            $numRows=count($this->arrCell) / $this->cols;
            $numRows=ceil($numRows);
            for($c=0;$c<$this->cols;$c++)
            {
                for($r=0;$r<$numRows;$r++)
                {
                    $arrTr[$c][]=$this->arrCell[$r*$this->cols + $c];
                }
            }
        }
        else
        {
            $numRows=count($this->arrCell) / $this->cols;
            $numRows=ceil($numRows);
            for($r=0;$r<$numRows;$r++)
            {
                for($c=0;$c<$this->cols;$c++)
                {
                    $arrTr[$r][]=$this->arrCell[$r*$this->cols + $c];
                }
            }
        }
        //trace($arrTr);
        foreach($arrTr as $rowNum=>$arrCol)
        {
            $tr="";
            foreach($arrCol as $ind=>$objCol)
            {
                $tr.=$objCol->render();
            }
            $table="{$table}
                <tr>{$tr}</tr>";
        }
        /*foreach($this->arrCell as $ind=>$objCell)
        {
            if(($ind % $this->cols)==0)
            {
                $table.="<tr>";
            }

            $table.=$objCell->render();

            if(($ind % $this->cols)==$this->cols-1)
            {
                $table.="</tr>";
            }
        }*/
        $table.="</table>";
        /*if($isCaptInclude==true && $this->captions!=null)
        {
            for($i=0;$i<$this->cols;$i++)
            {
                if(($i % $this->cols)==0)
                {
                                $table.="<tr>";
                }
                $table.="<th bgcolor='$this->headColor' id='R0C$i'>";
                $table.=$this->captions[$i];
                $table.="</th>";
                if(($i % $this->cols)==$this->cols-1)
                {
                                $table.="</tr>";
                }
            }
        }
        $r=0;
        $c=0;
        for($i=0;$i<count($this->values);$i++)
        {
            if(($i % $this->cols)==0)
            {
                $table.="<tr>";
                $r++;
                $c=0;
            }
            $tmpp="R$r"."C$c";
            $c++;
            $table.="<td  id='$tmpp'>";
            $table.=$this->values[$i];
            $table.="</td>";
            if(($i % $this->cols)==$this->cols-1)
            {
                $table.="</tr>";
            }
        }
        $table.="</table>";*/
        return $table;
    }
}
class ClsLHtmlcell
{
    private $isColHeading=false;
    private $isSideHeading=false;
    private $data="";
    private $arrParam=array();
    public function __construct()
    {
    }
    public function setAsColHeading()
    {
        $this->isColHeading=true;
    }
    public function setAsSideHeading()
    {
        $this->isSideHeading=true;
    }
    public function addData($data)
    {
        $this->data=$data;
    }
    public function addParam($name,$value)
    {
        $this->arrParam[$name]=$value;
    }
    public function getParam($name)
    {
        if(isset($this->arrParam[$name]))
        {
            return $this->arrParam[$name];
        }
        return false;
    }
    public function render()
    {
        $param="";
        if($this->arrParam)
        {
            foreach($this->arrParam as $k=>$v)
            {
                $param=$param." {$k}='{$v}'";
            }
        }
        if($this->isColHeading)
            return "<th{$param}>{$this->data}</th>";
        else
            return "<td{$param}>{$this->data}</td>";
    }
}
?>